<?php
class gas extends admin{

	private $table_gas = 'syt_gas'; //设备登记表
	private $table_car = 'syt_set'; //车辆设备号

    //添加加油记录
	public function add_gas($data){

		return $this->set_db($this->table_gas, $data);

	}



	/**
	 * 根据条件查询作业点设备的登记列表
	 *
	 * @param mixed $data
	 * @param int $page
	 * @param int $pagesize
	 */
	public function get_list($data,$page=1,$pagesize=10,$cols='*'){
		if((!is_numeric($page) || !is_numeric($pagesize)) && !$pagesize===false){
			return false;
		}

		$sql = "SELECT {$cols} FROM {$this->table_gas} WHERE 1 " ;

		//状态
		$sql.= " AND state={$data['state']} " ;

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']})";
		}

		//根据工程的id查询
		if($data['pid']){
			$sql .= " AND pid='{$data['pid']}'";//必须选择项目
		}

		//根据设备号
		if($data['no']){
			$sql .= " AND no like '%{$data['no']}%'";
		}

		//根据车辆类型
		if($data['cartype']){
			$sql .= " AND cartype like '%{$data['cartype']}%'";
		}

		//油罐编号
		if($data['gascan']){
			$sql .= " AND gascan like '%{$data['gascan']}%'";
		}

		//加油车编号
		if($data['carno']){
			$sql .= " AND carno like '%{$data['carno']}%'";
		}


		//油料管理员编号
		if($data['gaser']){
			$sql .= " AND gaser like '%{$data['gaser']}%'";
		}


		//类型  1-加油  2-进油
		if($data['type']){
			$sql .= " AND type={$data['type']} ";
		}

		//时间  是拍照的时间
		if($data['start_time']){
			$sql .= " AND ctime>=".(strtotime($data['start_time']));
		}
		if($data['end_time']){
			$sql .= " AND ctime<".(strtotime($data['end_time'])+86400);
		}

        if($data['isdate']){
            if($data['date']) {
                $starttime = strtotime($data['date']);
                $sql.=" AND ctime>={$starttime}";
            }

            if($data['date_end']){
                $endtime = strtotime($data['date_end']);
                $sql.=" AND ctime<{$endtime}";
            }
        }

		$sql .= " ORDER BY ctime DESC";

		if($page === false && $pagesize===false){
			return $this->_mysql(0)->find($sql);
		}

		if(1==$data['report']){
			return $this->_mysql(0)->find($sql);
		}



		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
	}

	//查询条件
	public function conf($data){

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group']){
			$sql .= " AND (xid={$info['id']} OR xid={$info['master']})";
		}

		//根据工程的id查询
		if($data['pid']){
			$sql .= " AND pid='{$data['pid']}'";//必须选择项目
		}

		//根据设备号
		if($data['no']){
			$sql .= " AND no like '%{$data['no']}%'";
		}

		//油罐编号
		if($data['gascan']){
			$sql .= " AND gascan like '%{$data['gascan']}%'";
		}

		//加油车编号
		if($data['carno']){
			$sql .= " AND carno like '%{$data['carno']}%'";
		}


		//油料管理员编号
		if($data['gaser']){
			$sql .= " AND gaser like '%{$data['gaser']}%'";
		}


		//类型  1-加油  2-进油
		if($data['type']){
			$sql .= " AND type={$data['type']} ";
		}

		//时间  是拍照的时间
		if($data['start_time']){
			$sql .= " AND ctime>=".(strtotime($data['start_time']));
		}
		if($data['end_time']){
			$sql .= " AND ctime<".(strtotime($data['end_time'])+86400);
		}

		return $sql;
	}


	//导出统计报表
	public function get_report($data){

		$sql = "SELECT carno,downer,COUNT(id) AS number,sum(num_all) as gasall,sum(`sum`) as gassum FROM {$this->table_gas} WHERE state=1 " ;

        $sql .= $this->conf($data);

		$sql .= " GROUP BY carno";

		return $this->_mysql(0)->find($sql);
	}


	//导出统计报表
	public function get_in_report($data){

		$sql = "SELECT gascan,COUNT(id) AS number,sum(num_all) as gasall,sum(`sum`) as gassum FROM {$this->table_gas} WHERE state=1 " ;

        $sql .= $this->conf($data);

		$sql .= " GROUP BY gascan";

		return $this->_mysql(0)->find($sql);
	}


	//查找加油车辆的产权人，产权人电话
	public function getCarinfo($no,$uid,$type=7){
		if(empty($no) || empty($uid))return false;

		$sql="select id,comment as name,phone,cartype from {$this->table_car} where uid={$uid} AND code='{$no}' AND type={$type}";
		return $this->_mysql(0)->findone($sql);
	}


	//查看加油信息
	public function getGas($id,$cols="*"){
		if(!is_numeric($id) || !$id)return false;

		$sql="select {$cols} from {$this->table_gas} where id={$id}";
		return $this->_mysql(0)->findone($sql);
	}

	//修改记录
	public function editGas($id,$data){
		if(!is_numeric($id))return false;

		$where="id={$id}";
		return $this->set_db($this->table_gas, $data,false,$where);
	}


	/**
	 * 记录重复提交验证，一个设备同一时间只能上传一条数据,通过设备号和时间进行判断
	 * @param string $no
	 * @param int $ctime
	 * @return bool true-重复  false-不重复
	 */
	public function isRepeat($no,$ctime){
		$sql="select id from {$this->table_gas} where no='{$no}' and ctime={$ctime}";
		$res=$this->_mysql(0)->findone($sql);
		return $res?true:false;
	}


    /**
     * 根据条件查询作业点设备的登记列表
     *
     * @param mixed $data
     * @param int $page
     * @param int $pagesize
     */
    public function get_list_all($data,$cols='*'){
        $sql = "SELECT {$cols} FROM {$this->table_gas} WHERE 1 " ;

        //状态
        $sql.= " AND state={$data['state']} " ;

        //根据会员id查询记录，总管理查看全部记录
        $admin_obj = new admin();
        $info = $admin_obj->out_cookie();
        if(empty($info)){return false;}
        if(6!=$info['group']){
            $sql .= " AND (xid={$info['id']} OR xid={$info['master']})";
        }

        //根据工程的id查询
        if($data['pid']){
            $sql .= " AND pid='{$data['pid']}'";//必须选择项目
        }

        //根据设备号
        if($data['no']){
            $sql .= " AND no like '%{$data['no']}%'";
        }

        //根据车辆类型
        if($data['cartype']){
            $sql .= " AND cartype like '%{$data['cartype']}%'";
        }

        //油罐编号
        if($data['gascan']){
            $sql .= " AND gascan like '%{$data['gascan']}%'";
        }

        //加油车编号
        if($data['carno']){
            $sql .= " AND carno like '%{$data['carno']}%'";
        }


        //油料管理员编号
        if($data['gaser']){
            $sql .= " AND gaser like '%{$data['gaser']}%'";
        }


        //类型  1-加油  2-进油
        if($data['type']){
            $sql .= " AND type={$data['type']} ";
        }

        //时间  是拍照的时间
        if($data['start_time']){
            $sql .= " AND ctime>=".(strtotime($data['start_time']));
        }
        if($data['end_time']){
            $sql .= " AND ctime<".(strtotime($data['end_time'])+86400);
        }

        //按日期查询
        if($data['date']){
            $time_start=strtotime($data['date']);
            $time_end=$time_start+86400;
            $sql.=" AND ctime>={$time_start} AND ctime<{$time_end} ";
        }

        $sql .= " ORDER BY ctime DESC";

        if(1==$data['report']){
            return $this->_mysql(0)->find($sql);
        }



        return $this->_mysql(0)->find($sql);
    }


}
